package net.godcode.t5c.components;

import org.apache.tapestry.ClientElement;
import org.apache.tapestry.ComponentResources;
import org.apache.tapestry.MarkupWriter;
import org.apache.tapestry.PageRenderSupport;
import org.apache.tapestry.annotations.AfterRender;
import org.apache.tapestry.annotations.BeginRender;
import org.apache.tapestry.annotations.Environmental;
import org.apache.tapestry.annotations.IncludeJavaScriptLibrary;
import org.apache.tapestry.annotations.Mixin;
import org.apache.tapestry.annotations.SupportsInformalParameters;
import org.apache.tapestry.corelib.mixins.RenderInformals;
import org.apache.tapestry.ioc.annotations.Inject;

@SupportsInformalParameters
@IncludeJavaScriptLibrary("clicksubmit.js")
/**
 * LinkSubmit allows arbitrary page (DOM) elements to submit a form.
 * 
 * @author Chris Lewis Jan 8, 2008 <chris@thegodcode.net>
 * @version $Id: LinkSubmit.java 86 2008-01-08 15:09:36Z burningodzilla $
 * @since 0.2.0
 */
public class LinkSubmit implements ClientElement {
	
	@SuppressWarnings("unused")
	@Mixin
	private RenderInformals renderInformals;
	
	@Inject
	private ComponentResources resources;
	
	@Environmental
	private PageRenderSupport renderSupport;
	
	/** The client-side id. */
	private String clientId;
	
	/* Render phase. */
	
	@BeginRender
	void beginRender(final MarkupWriter writer) {
		clientId = renderSupport.allocateClientId(resources.getId());
		renderSupport.addScript("new ClickSubmit('%s');", getClientId());
				writer.element("a", "id", getClientId(), "href", "#");
	}
	
	@AfterRender
	void afterRender(final MarkupWriter writer) {
		writer.end();
	}
	
	/* End render phase. */
	
	public String getClientId() {
		return clientId;
	}
	
}